﻿using System.Collections.Generic;

namespace EmbeddedSharp.Core {
    public abstract class Node : INode {

        public virtual string Value {
            get;
            set;
        }

        public abstract object Evaluate();

        public abstract object Accept(IVisitor visitor);

        public virtual string Type {
            get {
                return "<N/A>";
            }
        }

        /// <summary>
        /// Print the tree out in screen
        /// </summary>
        /// <param name="node"></param>
        public void PrintTree() {
            Node node;
            Queue<Node> queue = new Queue<Node>();
            queue.Enqueue(this);

            do {
                node = queue.Dequeue();
                if (node.GetType().IsSubclassOf(typeof(FuncNode))) {
                    FuncNode fn = (FuncNode)node;
                    foreach (Node child in fn.Parameters) {
                        queue.Enqueue(child);
                    }
                }
                //else if (node.GetType().IsSubclassOf(typeof(OperatorNode)))
                //{
                //    queue.Enqueue(((OperatorNode)node).Left);
                //    queue.Enqueue(((OperatorNode)node).Right);
                //}
            }
            while (queue.Count > 0);
        }

        public override string ToString() {
            DefaultExpressionTranslator dec = new DefaultExpressionTranslator();
            return dec.Translate(this);
        }
    }
}
